﻿using Antlr3.ST;
  
using JHECIS.PassOrder.BDET;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreateCode
{
    public partial class Form1 : Form
    {
        private string ParentXml = "";
        private string code = "";
        public Form1()
        {
            InitializeComponent();
            using (Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("CreateCode.Temp.Parent.xml"))
            {
                ParentXml = new StreamReader(sm).ReadToEnd();
            }
            using (Stream sm2 = Assembly.GetExecutingAssembly().GetManifestResourceStream("CreateCode.Temp.Code.txt"))
            {
                code = new StreamReader(sm2).ReadToEnd();
            }
        }
        public StringTemplateGroup StGroup { get; private set; }


        private void button1_Click(object sender, EventArgs e)
        {
            string data = "weqwqeqeqeq<PrintStatus>12</PrintStatus>55555";
            string pattern = "<PrintStatus>([.\\s\\S]*?)</PrintStatus>";
            var match = Regex.Match(data, pattern);
            string Code = match.Groups[1].Value;
           
            string xml = BuildModel();
            string fullname = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "1.xml");
            File.WriteAllText(fullname, xml);
        }

        #region 处理方法
        /// <summary>
        /// 整理入参实体
        /// </summary>
        /// <returns></returns>
        internal string BuildModel()
        {

            AuditModel model = new AuditModel();
            model.PatientName = "1";
            model.PatientId = "2";
            model.PatientCode = "3";//可空
            model.PatientNumber = "4";
            model.PatientPhone = "5";//可空
            model.PatientMail = "6";//可空
            model.PatientSex = "7";
            model.PatientBirthday = "8"; ;
            model.FeeItems = new List<FeeItem>();
            for (int i = 0; i < 2; i++)
            {

                FeeItem feeItem = new FeeItem();
                #region 数据
                feeItem.PrescriptionId = "q";
                feeItem.PrescriptionSerialNo = "w";//可空
                feeItem.DoctorDepartmentId = "e";
                feeItem.DoctorDepartmentName = "r";//---------
                feeItem.DoctorLevel = "";//======
                feeItem.DoctorId = "t";
                feeItem.DoctorName = "u";
                feeItem.PrescriptionDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                feeItem.SpecialCrowd = "i";//可空
                feeItem.Weight = "";//可空
                feeItem.Height = "";//可空
                feeItem.BodyArea = "";//可空;
                feeItem.PrescriptionType = "急诊";
                feeItem.PrescriptionMark = "";//可空;
                feeItem.PrescriptionIsAudit = "1";//-------------待讨论
                feeItem.TotalPrice = "";//可空;
                feeItem.DispenseDepartmentId = "";//可空;
                feeItem.DispenseDepartmentName = "";//可空;
                feeItem.DispenseDoctorId = "";//可空;
                feeItem.DispenseDoctorName = "";//可空;
                feeItem.DispenseDate = "";//可空;
                feeItem.Insurance = "";//可空;
                feeItem.Remark = "";
                feeItem.DiagnosisDesc = "";//可空 
                #endregion
                //药品
                feeItem.Drugs = new List<Drug>();
                for (int j = 0; j < 3; j++)
                {

                    Drug drug = new Drug();
                    #region 药品
                    drug.ItemId = "";
                    drug.ItemType = "rrtt";
                    drug.Frequency = "trtrtr";
                    drug.Condition = "";//可空
                    drug.Route = "trtrtr";
                    drug.SingleDose = "";
                    drug.SingleDoseUnit = "trtrtr";
                    drug.Quantity = "";
                    drug.QuantityUnit = "";
                    drug.Days = "";//可空
                    drug.DrugGroup = "";//可空
                    drug.SkinTest = "";
                    drug.SkinTestResult = "";//可空
                    drug.DrugOrder = "";//可空
                    drug.DrugRemark = "";//可空
                    drug.Destinations = "";//可空
                    drug.ItemComName = "";
                    drug.ItemProName = "";//可空
                    drug.Formulation = "";//可空
                    drug.Manufacture = "";//可空
                    drug.Packing = "";//可空
                    drug.Specs = "";//可空
                    drug.UnitPrice = "";//可空
                    drug.TotalPrice = "";//可空
                    drug.InstructDrugTime = "";//可空
                    drug.InstructDrugLeftTime = "";//可空
                    drug.InstructDrugRightTime = "";//可空
                    drug.InstructDrugSpeed = "";//可空
                    drug.InstructDrugSpeedUnit = "";//可空 
                    #endregion
                    feeItem.Drugs.Add(drug);
                }
                //诊断
                feeItem.Diagnosises = new List<Diagnosis>();
                for (int h = 0; h < 3; h++)
                {
                    Diagnosis diagnosis = new Diagnosis();
                    diagnosis.Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    diagnosis.Name = "测试诊断";
                    feeItem.Diagnosises.Add(diagnosis);
                }
              
                model.FeeItems.Add(feeItem);

            }
            return BuildXml<AuditModel>(ParentXml, model);
        }

        /// <summary>
        /// 整理入参xml
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal string BuildXml<T>(string xml, T model)
        {
            AuditModel m = JsonConvert.DeserializeObject<AuditModel>(code);
           
            StringTemplate st = new StringTemplate(xml);
            st.SetAttribute("Ct", m);
            return st.ToString();
        }

        #endregion
    }
}
